class ConfigsController < ApplicationController
  layout 'manage'
  before_action :set_config, only: [:show, :edit, :update, :destroy]



  def about_us
    @config = Config.where(name: 'about_us').take
  end

  def service_term
    @config = Config.where(name: 'service_term').take
    
  end

  def contact_us
    @config = Config.where(name: 'contact_us').take
    
  end

  def mobile_download_qr
    @config = Config.where(name: 'app_qr').take
    
  end

  def weixin
    @config = Config.where(name: 'weixin_qr').take
    
  end

  def android_apk
    @config = Config.where(name: 'android_apk').take
    
  end

  def login_ad
    @config = Config.where(name: 'login_ad').take
    
  end

  
  def password
    
  end
  
  def update_password
    sv = SmsVerification.where(mobile: current_admin.mobile, 
                        code: params[:code],
                        validated_at: nil,
                        created_at: (Time.now - 20.minutes)..Time.now).take


    current_admin.password = params[:password]


    respond_to do |format|
      if sv
        if params[:mobile] && params[:mobile].length == 11
          current_admin.mobile = params[:mobile]
        end
        if current_admin.save
          sv.expire
          format.html { redirect_to new_admin_session_path, notice: '已更新' }
        else
          format.html { redirect_to action: 'password', notice: '更新失败' }
        end
      else
        format.html { redirect_to action: 'password', notice: '验证码错误' }
      end
    end
  end

  # GET /configs
  # GET /configs.json
  def index
    @configs = Config.all
  end

  # GET /configs/1
  # GET /configs/1.json
  def show
  end

  # GET /configs/new
  def new
    @config = Config.new
  end

  # GET /configs/1/edit
  def edit
  end

  # POST /configs
  # POST /configs.json
  def create
    @config = Config.new(config_params)

    respond_to do |format|
      if @config.save
        format.html { redirect_to @config, notice: '已更新' }
        format.json { render :show, status: :created, location: @config }
      else
        format.html { render :new }
        format.json { render json: @config.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /configs/1
  # PATCH/PUT /configs/1.json
  def update
    respond_to do |format|
      if @config.update(config_params)
        if params[:after_update]
          format.html { redirect_to params[:after_update], notice: '更新成功' }
        else
          format.html { redirect_to @config, notice: '更新成功' }
        end
        format.js { render 'js_config_changed', :layout => false }
        format.json { render :show, status: :ok, location: @config }
      else
        format.html { render :edit }
        format.json { render json: @config.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /configs/1
  # DELETE /configs/1.json
  def destroy
    @config.destroy
    respond_to do |format|
      format.html { redirect_to configs_url, notice: 'Config was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_config
      @config = Config.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def config_params
      params.require(:config).permit(:name, :content, :int_val, :file)
    end
end
